package com.pipayshop.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.pipayshop.entity.Configuration;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.service.ConfigurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zxb
 * @since 2023-08-08
 */
@RestController
@Api(value = "配置接口",tags = "配置接口")
@RequestMapping("/pipayshopapi/configuration")
public class ConfigurationController {

    @Autowired
    private ConfigurationService configurationService;

    @GetMapping("getEnabledPi")
    @ApiOperation("是否开启PI汇率自动转换")
    public boolean getEnabledPi() {
        Configuration configuration = configurationService.getOne(new LambdaQueryWrapper<Configuration>()
                .eq(Configuration::getContent, "pi")
                .eq(Configuration::getDelFlag, 0));
        return configuration != null;
    }

    @GetMapping("getEshopLimitPrice")
    @ApiOperation("获取提升网店商品额度价格")
    public ResponseVO<String> getEshopLimitPrice() {
        Configuration configuration = configurationService.getOne(new LambdaUpdateWrapper<Configuration>()
                .eq(Configuration::getContent, "eshop_limit_price")
                .eq(Configuration::getDelFlag, 0));
        return ResponseVO.getSuccessResponseVo(configuration.getConfigValue());

    }

    @GetMapping("getshopLimitPrice")
    @ApiOperation("获取提升实体店额度价格")
    public ResponseVO<String> getShopLimitPrice() {
        Configuration configuration = configurationService.getOne(new LambdaUpdateWrapper<Configuration>()
                .eq(Configuration::getContent, "shop_limit_price")
                .eq(Configuration::getDelFlag, 0));
        return ResponseVO.getSuccessResponseVo(configuration.getConfigValue());

    }
    @PostMapping("setValueByKey")
    @ApiOperation("根据key修改value")
    public ResponseVO<String> setValueByKey(String key,String value) {
        boolean flag = configurationService.setValueByKey(key, value);
        if (!flag) {
            throw new RuntimeException();
        }
        return ResponseVO.getSuccessResponseVo("update_success");

    }
}
